﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Image = System.Drawing.Image;

namespace Demo
{
    public enum ImageType { Png, Gif, Jpg, Bmp };

    public class ImageUtility
    {
        public static ImageFormat GetFormatFromImageType(ImageType imageType)
        {
            switch(imageType)
            {
                case ImageType.Jpg:
                    return ImageFormat.Jpeg;
                case ImageType.Png:
                    return ImageFormat.Png;
                case ImageType.Gif:
                    return ImageFormat.Gif;
                case ImageType.Bmp:
                    return ImageFormat.Bmp;
                default:
                    throw new InvalidOperationException("Invalid format " + imageType + ".");
            }
        }

        public static ImageType GetImageTypeFromExtension(String extension)
        {
            switch (extension.ToLower())
            {
                case ".bmp":
                    return ImageType.Bmp;
                case ".gif":
                    return ImageType.Gif;
                case ".jpg":
                case ".jpeg":
                    return ImageType.Jpg;
                case ".png":
                    return ImageType.Png;
                default:
                    throw new ArgumentOutOfRangeException("Invalid format " + extension + ".");
            }
        }

        public static String GetContentTypeFromImageType(ImageType imageType)
        {
            switch (imageType)
            {
                case ImageType.Png:
                    return "image/png";
                case ImageType.Jpg:
                    return "image/jpeg";
                case ImageType.Gif:
                    return "image/gif";
                case ImageType.Bmp:
                    return "image/bmp";
                default:
                    throw new ArgumentOutOfRangeException("imageType"); 
            }
        }

        public static String GetContentTypeFromExtension(String extension)
        {
            switch (extension.ToLower())
            {
                case ".fif":
                    return "application/fractals";
                case ".hta":
                    return "application/hta";
                case ".hqx":
                    return "application/mac-binhex40";
                case ".vsi":
                    return "application/ms-vsi";
                case ".p10":
                    return "application/pkcs10";
                case ".p7m":
                    return "application/pkcs7-mime";
                case ".p7s":
                    return "application/pkcs7-signature";
                case ".cer":
                    return "application/pkix-cert";
                case ".crl":
                    return "application/pkix-crl";
                case ".ps":
                    return "application/postscript";
                case ".setpay":
                    return "application/set-payment-initiation";
                case ".setreg":
                    return "application/set-registration-initiation";
                case ".sst":
                    return "application/vnd.ms-pki.certstore";
                case ".pko":
                    return "application/vnd.ms-pki.pko";
                case ".cat":
                    return "application/vnd.ms-pki.seccat";
                case ".stl":
                    return "application/vnd.ms-pki.stl";
                case ".wpl":
                    return "application/vnd.ms-wpl";
                case ".xps":
                    return "application/vnd.ms-xpsdocument";
                case ".z":
                    return "application/x-compress";
                case ".tgz":
                    return "application/x-compressed";
                case ".gz":
                    return "application/x-gzip";
                case ".ins":
                    return "application/x-internet-signup";
                case ".iii":
                    return "application/x-iphone";
                case ".jtx":
                    return "application/x-jtx+xps";
                case ".latex":
                    return "application/x-latex";
                case ".nix":
                    return "application/x-mix-transfer";
                case ".asx":
                    return "application/x-mplayer2";
                case ".application":
                    return "application/x-ms-application";
                case ".wmd":
                    return "application/x-ms-wmd";
                case ".wmz":
                    return "application/x-ms-wmz";
                case ".xbap":
                    return "application/x-ms-xbap";
                case ".p12":
                    return "application/x-pkcs12";
                case ".p7b":
                    return "application/x-pkcs7-certificates";
                case ".p7r":
                    return "application/x-pkcs7-certreqresp";
                case ".sit":
                    return "application/x-stuffit";
                case ".tar":
                    return "application/x-tar";
                case ".man":
                    return "application/x-troff-man";
                case ".zip":
                    return "application/x-zip-compressed";
                case ".xaml":
                    return "application/xaml+xml";
                case ".xml":
                    return "application/xml";
                case ".au":
                    return "audio/basic";
                case ".mid":
                    return "audio/mid";
                case ".mp3":
                    return "audio/mp3";
                case ".m3u":
                    return "audio/mpegurl";
                case ".wav":
                    return "audio/wav";
                case ".aiff":
                    return "audio/x-aiff";
                    return "audio/x-mpegurl";
                case ".wax":
                    return "audio/x-ms-wax";
                case ".wma":
                    return "audio/x-ms-wma";
                case ".bmp":
                    return "image/bmp";
                case ".gif":
                    return "image/gif";
                case ".jpg":
                    return "image/jpeg";
                case ".png":
                    return "image/png";
                case ".tiff":
                    return "image/tiff";
                case ".ico":
                    return "image/x-icon";
                case ".dwfx":
                    return "model/vnd.dwfx+xps";
                case ".css":
                    return "text/css";
                case ".323":
                    return "text/h323";
                case ".htm":
                    return "text/html";
                case ".uls":
                    return "text/iuls";
                case ".txt":
                    return "text/plain";
                case ".wsc":
                    return "text/scriptlet";
                case ".htt":
                    return "text/webviewhtml";
                case ".htc":
                    return "text/x-component";
                case ".vcf":
                    return "text/x-vcard";
                case ".avi":
                    return "video/avi";
                case ".mpeg":
                    return "video/mpeg";
                case ".wm":
                    return "video/x-ms-wm";
                case ".wmv":
                    return "video/x-ms-wmv";
                case ".wmx":
                    return "video/x-ms-wmx";
                case ".wvx":
                    return "video/x-ms-wvx";
                default:
                    throw new InvalidOperationException("Invalid format " + extension + ".");
            }
        }

        public static void ResizeImage(ImageType imageType, byte[] data, int width, int height, Stream output)
        {
            using (MemoryStream stream = new MemoryStream(data))
            {
                using (Image image = Image.FromStream(stream, false, false))
                {
                    // Prevent using images internal thumbnail
                    image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                    image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

                    using (System.Drawing.Image newImage = image.GetThumbnailImage(width, height, null, IntPtr.Zero))
                    {
                        newImage.Save(output, GetFormatFromImageType(imageType));
                    }
                }
            }
        }
    }
}